using System;
using System.Collections.Generic;
using System.Text;

using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;


namespace visitexpression
{
    class visitexpression
    {
        static void Main(string[] args)
        {
            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVOracle);//DbVOracle
//            sqlparser.SqlText.Text = @"SELECT f1 FROM   t1 WHERE  t1.a = 10.2        AND name = 'k'        OR id IN ( 2, 4, 5 ) HAVING MAX(x) > 6 ";
//            sqlparser.SqlText.Text = @"SELECT f1 FROM   t1 WHERE (substring([name],(1),(1))='P' OR len([name])>(2) AND len([name])>(4) AnD (len(name)>5 OR len(name)<4)) ";
            sqlparser.SqlText.Text = @"SELECT DISTINCT UNIPRT.DOC_REQUEST.REQUEST_ID,
                TEMPLATE_ID,
                'C'
                || UNIPRT.CORRESPONDENCE_CONDITIONS.CONDITION_ID,
                DESCRIPTION,
                RESULT
FROM   UNIPRT.DOC_REQUEST,
       UNIPRT.CORRESPONDENCE_CONDITIONS,
       UNIPRT.CONDITIONS,
       UNIPRT.CONDITION_STATEMENTS
WHERE      UNIPRT.DOC_REQUEST.REQUEST_ID = UNIPRT.CORRESPONDENCE_CONDITIONS.REQUEST_ID(+)
       AND UNIPRT.CORRESPONDENCE_CONDITIONS.CONDITION_ID = UNIPRT.CONDITIONS.CONDITION_ID
       AND UNIPRT.CORRESPONDENCE_CONDITIONS.CONDITION_ID = UNIPRT.CONDITION_STATEMENTS.CONDITION_ID(+)
       AND UNIPRT.DOC_REQUEST.TEMPLATE_ID = :template
       AND UNIPRT.DOC_REQUEST.PROCESS_DATE > To_date('01/05/2010', 'DD/MM/YYYY')";


            Console.WriteLine("Processing..");

            int i = sqlparser.Parse();
            if (i == 0)
            {
               visitbinaryexprtree(sqlparser.SqlStatements[0].WhereClause);
            }
            else
                Console.WriteLine(sqlparser.ErrorMessages);
        }

        public static void visitbinaryexprtree(TLzCustomExpression pExpr)
        {
            Console.WriteLine("pre-order:");
            pExpr.PreOrderTraverse(treenodevisitor);
            Console.WriteLine("in-order:");
            pExpr.InOrderTraverse(treenodevisitor);
            Console.WriteLine("post-order:");
            pExpr.PostOrderTraverse(treenodevisitor);

        }

        public static Boolean treenodevisitor(TLz_Node pnode, Boolean pIsLeafNode)
        {
            TLzCustomExpression lcexpr;
            String outstr;
            if (pIsLeafNode)
            {
                Console.WriteLine(pnode.AsText);
            }
            else
            {
                lcexpr = (TLzCustomExpression)pnode;
                outstr = lcexpr.oper.ToString();
                if (lcexpr.lexpr != null)
                {
                    outstr = outstr + "\nleft:\t" + lcexpr.lexpr.AsText;
                }

                if (lcexpr.rexpr != null)
                {
                    outstr = outstr + "\nreft:\t" + lcexpr.rexpr.AsText;
                }

                Console.WriteLine(outstr+"\n\n");
                
                /*
                if (lcexpr.opname != null){
                    Console.WriteLine("opname: "+lcexpr.opname.AsText+", "+lcexpr.oper.ToString()+", "+lcexpr.AsText);
                }else{
                    Console.WriteLine(lcexpr.oper.ToString() + ", " + lcexpr.AsText);
                }
                 */
 
            }
            return true;
        }

    }
}
